package algorithm.poj.p1000;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLDecoder;
import java.util.StringTokenizer;

public class P1026 {

	public static void main(String[] args) throws Exception {

		InputStream input = null;
		if (false) {
			input = System.in;
		} else {
			URL url = P1026.class.getResource("P1026.txt");
			File file = new File(URLDecoder.decode(url.getPath(), "UTF-8"));
			input = new FileInputStream(file);
		}
		
		BufferedReader stdin = new BufferedReader(new InputStreamReader(input));

		String line = stdin.readLine();
		StringTokenizer st = null;
		while (line != null && !"0".equals(line)) {
			int n = Integer.valueOf(line);
			int[] a = new int[n];
			st = new StringTokenizer(stdin.readLine());
			for (int i = 0; i < n; i ++) {
				a[i] = Integer.valueOf(st.nextToken())-1;
			}
			int[] c = cycle(a);
			String s = stdin.readLine();
			while (!"0".equals(s)) {
				int index = s.indexOf(" "); 
				long k = Long.valueOf(s.substring(0, index).trim());
				String m = s.substring(index+1);
				while (m.length() < n) m = m + " ";
				char[] cs = m.toCharArray();
				System.out.println(new String(encode(cs, cycle(a, c, k))));
				s = stdin.readLine();
			}
			System.out.println();
			line = stdin.readLine();
		}
	}
	
	private static int[] cycle(int[] a) {
		
		int[] c = new int[a.length];
		for (int i = 0; i < a.length; i ++) {
			int x = a[i];
			int s = 1;
			while (x != i) {
				x = a[x];
				s ++;
			}
			c[i] = s;
		}
		return c;
	}
	
	private static int[] cycle(int[] a, int[] c, long k) {
		
		int[] t = new int[a.length];
		for (int i = 0; i < a.length; i ++) {
			t[i] = a[i];
			int l = (int)((k-1)%c[i]);
			while (l > 0) {
				t[i] = a[t[i]];
				l --;
			}
		}
		return t;
	}
	
	private static char[] encode(char[] cs, int[] a) {
		
		char[] t = new char[cs.length];
		for (int i = 0; i < t.length; i ++) {
			t[a[i]] = cs[i];
		}
		return t;
	}
}